package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.IdBooleanRequest;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.OpenPageRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.AddOpenPageDTO;
import com.sc.nft.dto.UpdateOpenPageDTO;
import com.sc.nft.entity.vo.AdminOpenPageVO;
import com.sc.nft.service.OpenPageService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 开屏页表控制层
 * @author tangzehui
 * @since 2023-11-23
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/openPage")
@Api(tags = "后台开屏页管理")
public class OpenPageController {


    private final OpenPageService openPageService;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取分页数据", notes = "获取分页数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminOpenPageVO> pageAdmin(@RequestBody OpenPageRequest request) {
        return PageResult.success(openPageService.pageAdmin(request.getFirstTitle(), request.getPageNo(), request.getPageSize()));
    }

    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteById(@RequestBody IdRequest idRequest) {
        openPageService.deleteById(idRequest.getId());
        return Result.success();
    }

    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result update(@Valid @RequestBody UpdateOpenPageDTO updateOpenPageDTO) {
        openPageService.updateById(updateOpenPageDTO.getId(), updateOpenPageDTO.getFirstTitle(), updateOpenPageDTO.getImage());
        return Result.success();
    }

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result add(@Valid @RequestBody AddOpenPageDTO addOpenPageDTO) {
        openPageService.add(addOpenPageDTO.getFirstTitle(), addOpenPageDTO.getImage());
        return Result.success();
    }

    @PostMapping(value = "/updateIsOpen", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改状态", notes = "修改状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateIsOpenById(@Valid @RequestBody IdBooleanRequest request) {
        openPageService.updateIsOpenById(request.getId(), request.getIsOpen());
        return Result.success();
    }
}
